const _ = require('lodash');
const asciidoctor = require('@asciidoctor/core')();
const hljsExt = require('asciidoctor-highlight.js');
const katexExt = require('asciidoctor-katex');
const moment = require('moment');
const S = require('string');

hljsExt.register(asciidoctor.Extensions); // Register the extension into global registry.
katexExt.register(asciidoctor.Extensions, { katexOptions: { macros: { '\\RR': '\\mathbb{R}' } } });

/**
 * @typedef AdocInfo
 * @property {{string}} title
 * @property {{string}} author
 * @property {{string}} version
 * @property {{number}} date
 * @property {{string[]}} tags
 *
 * @param {string} adocText
 * @return {{ content:string, adocInfo:AdocInfo }}
 */
function parse(adocText) {
  const doc = asciidoctor.load(adocText, {
    attributes: { icons: 'font', 'source-highlighter': 'highlightjs-ext', stem: 'latexmath' }
  });

  const revisionInfo = doc.getRevisionInfo();
  const adocInfo = {
    title: S(doc.getTitle()).decodeHTMLEntities().s,
    author: doc.getAttribute('author'),
    tags: (() => {
      const tag = doc.getAttribute('tag');
      return _.isEmpty(tag) ? [] : _.split(tag, ',');
    })(),
    version: revisionInfo.getNumber(),
    date: _.isEmpty(revisionInfo.getDate()) ? null : moment(revisionInfo.getDate()).valueOf()
  };
  const content = doc.convert();
  return { adocInfo, content };
}

module.exports = parse;
